function PrintTable(billList){
    var len = $(".waybillNum").length;
    var wayNum = "";
    for (var i = 0; i < len; i++) {
        wayNum += $(".waybillNum")[i].innerText + ",";
    }
    wayNum = wayNum.substring(0, wayNum.length - 1);
    //newPrintWayBill(billList, wayNum); //打印
    lodopPrintTable(billList, wayNum); //打印
}
//当前打印模板
var htmlTemplateObj = null;

function lodopPrintTable(billList, wayNum){
    //获取打印模板
    htmlTemplateObj = $("#printContext");
    //console.log("shipperName"+htmlTemplateObj.find("span.shipperName").text())
    var LODOP = null;
    try{
        LODOP=getLodop();
        if ((LODOP!=null)&&(typeof(LODOP.VERSION)!="undefined")){
            $("#lodopTips").hide();
        }
        else{
            $("#lodopTips").show();
            return;
        }
    }catch(err){
        $("#lodopTips").show();
        return;
    }
    //打印
    print(billList, wayNum);

}
function getValue(key,data){
    try {
        return new window.Function("__data","with(__data){return "+key+"}").call(data,data);
    }catch (err){
        return null
    }
}
function valueToString(value){
    if(value===null||value===void 0)return "";
    return String(value)
}
function getStringValue(value,data){
    return valueToString(value).replace(/{{(.*?)}}/g,function($1,$2){
        var text = getValue($2,data);
        if(text===null||typeof text==="undefined")return "";
        return text
    })
}
//打印模板
function templateDataProcess(data, index){
    if(!data)return null;
    data.transType = data.transType || '无法匹配';
    data.markerPen = data.markerPen || '无大头笔信息';
    data.printTime = new Date().toLocaleString();
    data.currentCargoIndex = data.cargoCount < 2 ? data.cargoCount : data.cargoCount + '-' + (index+1);
    data.reciveLoanAccount = (data.reciveLoanAccount || '').replace(/^(.{3})(.*)(.{4})$/g,'$1*$3');
    htmlTemplateObj.find('[data-text]').each(function () {
        var $this = $(this);
        if($this.parent().attr('data-type'))return;
        var text = getStringValue($this.attr('data-text'),data);
        $this.html(text.split('\\n').join('<br>'));
    });
    htmlTemplateObj.find('[data-show]').each(function () {
        var $this = $(this);
        var dataShow = $.trim($this.attr('data-show'));
        if(!dataShow){
            $this.show();
            return;
        }
        var show = getValue(dataShow,data);
        show ? $this.show():$this.hide();
    });
//	return htmlTemplateObj.html();
}
/**
 * 打印
 * @param list
 */
function print(billList, wayNum){
    for(var i=0; i<billList.length; i++){

        var data = billList[i];
        data.cargoCount = 1;
        for(var j=0; j<data.cargoCount; j++){

            var serialNo = data.printSerialNos;
            if(data.cargoCount>1){
                serialNo = data.printSerialNos.split(",")[j];
            }
            //条码
            var barcode = data.waybillNumber + serialNo + data.stationNumber;

            var isHideWareHouse  = data.isHideWareHouse;

            //模板数据处理
            templateDataProcess(data, j);
            console.log("XXXX: "+ htmlTemplateObj.find("span.shipperName").text())
            //打印电子运单1
            printEWaybill(data, serialNo, barcode, wayNum);

            //打印仓库分拣单
//			if("N"==isHideWareHouse){
//				printWareHouseWaybill(data, serialNo, barcode);
//			}

        }
    }
    if(billList.length>0){
        //$.myAlert("本次共打印"+billList.length+"张，已成功添加到打印列表！");

    }else {
        //$.myAlert('请先勾选需要打印的面单');
    }
}
/**
 * 打印电子运单
 * @param data
 * @param serialNo
 */
function printEWaybill(data, serialNo, barcode, wayNum){
    //打印内容
    var printHtml = htmlTemplateObj.html();

    LODOP.PRINT_INIT("");
    LODOP.SET_PRINT_PAGESIZE(1,760,1300,"");

    // LODOP.ADD_PRINT_TEXT(130,30,150,130,$(".markerPenCode").text());
    // LODOP.ADD_PRINT_TEXT(130,30,150,130,"河南郑州");
    //LODOP.SET_SHOW_MODE("BKIMG_WIDTH","100mm");
    //LODOP.SET_SHOW_MODE("BKIMG_HEIGHT","180mm");


    LODOP.ADD_PRINT_HTM(0,0,288,484,printHtml);
    htmlTemplateObj.find('[data-type]').each(function () {
        var $this = $(this);
        var type = $.trim($this.attr('data-type'));
        var text = getStringValue($.trim($this.find('[data-text]').attr('data-text')),data);
        if(type === 'barCode' || type === 'qrCode'){
            var offset = $.trim($this.attr('data-offset')).split(',');
            LODOP.ADD_PRINT_BARCODE(offset[0] || this.offsetTop,offset[1] || this.offsetLeft,offset[2] || this.offsetWidth,offset[3] || this.offsetHeight,type === 'barCode' ? "128A" : 'QRCode', text);
            LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);
        }
    });

    //打印
    //LODOP.PREVIEW();
    //LODOP.PRINT();
LODOP.PREVIEW();

    //记录打印日志
    try {
        printLogses(data);
    } catch (e) {
    }
}
function printTest() {
    var printOrder = {};

    printOrder.waybillNumber = $("#waybillNumber").val();
    printOrder.conCompany = $("#conCompany").val();
    printOrder.cargoPack = $("#cargoPack").val();
    printOrder.returnRequirement = $("#returnRequirement").val();
    printOrder.shipperTel = $("#shipperTel").val();
    printOrder.saddress = $("#saddress").val();
    printOrder.shipperPhone = $("#shipperPhone").val();
    printOrder.conPhone = $("#conPhone").val();
    printOrder.conTel = $("#conTel").val();
    printOrder.shipperName = $("#shipperName").val();
    printOrder.conName = $("#conName").val();
    printOrder.caddress = $("#caddress").val();
    printOrder.cargoName = $("#cargoName1").val();
    printOrder.refundPayment = $("#refundPayment").val();
    printOrder.payType = $("#payType").val();
    printOrder.transType = $("#transType").val();
    printOrder.destination = $("#destination").val();
    printOrder.arrivedOrgSimpleName = $("#arrivedOrgSimpleName").val();
    printOrder.billWeight = $("#billWeight").val();
    printOrder.transportCharge = $("#transportCharge").val();
    printOrder.totalCharge = $("#totalCharge").val();
    printOrder.insuranceMoney = $("#insuranceMoney").val();
    printOrder.signSheet = $("#signSheet").val();
    printOrder.outerField1 = $("#outerField1").val();
    printOrder.lastOutLoadOrgName = $("#lastOutLoadOrgName").val();
    printOrder.departCityName = $("#departCityName").val();
    printOrder.secondLoadOrgName = $("#secondLoadOrgName").val();
    printOrder.stationNumber = $("#stationNumber").val();
    printOrder.custOrderLine = $("#custOrderLine").val();
    printOrder.transNote = $("#transNote1").val();
    printOrder.markerPen = $("#markerPen1").val();
    printOrder.orderNumber = $("#orderNumber").val();
    printOrder.billTime = $("#billTime").val();
    printOrder.receiveMethod = $("#receiveMethod").val();
    printOrder.cargoCount = $("#cargoCount").val();
    printOrder.reciveLoanAccount = $("#reciveLoanAccount").val();
    printOrder.printSerialNos = $("#printSerialNos").val();
    printOrder.isHideWareHouse = $("#isHideWareHouse").val();
    printOrder.originalNumber = $("#originalNumber").val();


    var printList = [];
    printList.push(printOrder);
    PrintTable(printList);
}